/*
 * Created on Aug 26, 2011
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package demo.basic.employeedb.server;

import java.util.ArrayList;
import java.util.List;

import demo.basic.employeedb.core.DBException;
import demo.basic.employeedb.core.Employee;
import demo.basic.employeedb.core.EmployeeDB;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EmployeeDBService implements EmployeeDB {
	private List<Employee> employees = new ArrayList<Employee>();

	public void add(Employee e) throws DBException {
		if(e == null)
			throw new DBException("Add Employee Exception: Employee is NULL");
		if(!employees.contains(e)) {
			employees.add(e);
		} else {
			throw new DBException("Add Employee Exception: Employee's ID exists");
		}
	}

	public void remove(int id) throws DBException {
		if(employees.isEmpty()) {
			throw new DBException("Remove Employee Exception: No Employee with id="+id+" in DB");
		}
		for(int i=0;i<employees.size();i++) {
			Employee e = (Employee)employees.get(i);
			if(e.getId() == id) {
				employees.remove(i);
				return;
			}
		}
		throw new DBException("Remove Employee Exception: No Employee with id="+" in DB");
	}

	public Employee get(int id) throws DBException {
		if(employees.isEmpty()) {
			throw new DBException("Remove Employee Exception: No Employee in DB with id="+" in DB");
		}
		for(int i=0;i<employees.size();i++) {
			Employee e = (Employee)employees.get(i);
			if(e.getId() == id) {
				return e;
			}
		}
		throw new DBException("Remove Employee Exception: No Employee in DB");
	}

	public List<Employee> getAll() {
		return employees;
	}

}
